package com.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;

// k3 reduce输入的key，类型是Text
//v3 reduce输入的values 类型  IntWritable
//k4 reduce输出 类型 Text
//v4 reduce输出 类型IntWritable
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {

  private  IntWritable v4 = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //1. k4 = k3
        //2. v4 = v3元素的和：遍历v3求和
        int sum = 0;
        for (IntWritable value : values) {
          sum+=value.get();
        }
        v4.set(sum);
        //3.输出k4, v4：context.write()方法
        context.write(key,v4);

    }
}
